package eureka.service.account.impl;

import eureka.dal.DatabaseSession;
import eureka.dao.account.IAcceptableUserDao;
import eureka.dao.account.IWebsiteUserDao;
import eureka.dao.account.impl.AcceptableUserDao;
import eureka.dao.account.impl.WebsiteUserDao;
import eureka.domain.account.AcceptableUser;
import eureka.domain.account.UserRole;
import eureka.domain.account.UserState;
import eureka.domain.account.WebsiteUser;
import eureka.exceptions.DatabaseException;
import eureka.service.account.IAccountManagementService;

public class AccountManagementService implements IAccountManagementService {

	private IWebsiteUserDao websiteUserDao = new WebsiteUserDao();
	private IAcceptableUserDao acceptableUserDao =new  AcceptableUserDao();
	@Override
	public AcceptableUser getUserByAuID(long auID) throws DatabaseException {
		try(DatabaseSession dbses=new DatabaseSession())
		{
			AcceptableUser auser=acceptableUserDao.getUserByAuID(dbses, auID);
			return auser;
		}
	}

	@Override
	public void addAcceptableUser(AcceptableUser auser)
			throws DatabaseException {
		
		try(DatabaseSession dbses =new DatabaseSession())
		{
			acceptableUserDao.add(dbses, auser);
		}

	}

	@Override
	public boolean updateAcceptableUser(AcceptableUser auser)
			throws DatabaseException {
		try(DatabaseSession dbses =new DatabaseSession())
		{
		return acceptableUserDao.update(dbses, auser);
		}
	}

	@Override
	public boolean deleteAcceptableUserByAuID(long auID)
			throws DatabaseException {
		try(DatabaseSession dbses=new DatabaseSession())
		{
			return acceptableUserDao.deleteByAuID(dbses, auID);
		}
		
	}

	@Override
	public WebsiteUser getWebsiteUserByAuID(long auID) throws DatabaseException {
		try(DatabaseSession dbses=new DatabaseSession())
		{
			return websiteUserDao.getUserByAuID(dbses, auID);
		}
	}

	@Override
	public WebsiteUser getWebsiteUserByUID(long uID) throws DatabaseException {
		try(DatabaseSession dbses=new DatabaseSession())
		{
			return websiteUserDao.getUserByUID(dbses, uID);
		}
	}

	@Override
	public boolean updateWebsiteUser(WebsiteUser wuser)
			throws DatabaseException {
		try(DatabaseSession dbses=new DatabaseSession())
		{
			return websiteUserDao.forceUpdate(dbses, wuser);
		}
	}

	@Override
	public boolean deleteWebsiteUserByUID(long uID) throws DatabaseException {
		try(DatabaseSession dbses=new DatabaseSession())
		{
			return websiteUserDao.deleteByUID(dbses, uID);
		}
	}

	@Override
	public boolean getUserPermissions(long uID) throws DatabaseException {
			WebsiteUser wuser = getWebsiteUserByUID(uID);
			if(wuser.getRole() == UserRole.Guest || wuser.getState() != UserState.Normal)
				return false;
		return true;
	}
}
